package com.hr.quartz.task;


import com.hr.borrowing.domain.Borrowing;
import com.hr.borrowing.service.IBorrowingService;
import com.hr.borrowingLog.domain.BorrowingLog;
import com.hr.borrowingLog.service.IBorrowingLogService;
import com.hr.common.core.domain.entity.SysUser;
import com.hr.interview.domain.Interview;
import com.hr.interview.domain.InterviewDTO;
import com.hr.interview.service.IInterviewService;
import com.hr.jobTimeRecord.domain.JobTimeRecord;
import com.hr.jobTimeRecord.service.IJobTimeRecordService;
import com.hr.outTime.service.IOutTimeService;
import com.hr.system.domain.SysUserRole;
import com.hr.system.service.ISysRoleService;
import com.hr.system.service.ISysUserService;
import com.hr.system.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Component("hrTask")
public class HrTask {

    @Resource
    private IInterviewService interviewService;

    @Resource
    private ISysUserService userService;

    @Resource
    private IBorrowingService borrowingService;

    @Resource
    private ISysRoleService sysUserRoleService;

    @Resource
    private IJobTimeRecordService jobTimeRecordService;

    @Resource
    private IOutTimeService outTimeService;

    /**
     * 查询面试信息表的生成时间，并日当前日期做减法，如果大于意向等级对应的保护期，则修改面试状态
     */
    public void saveTime(){
        Interview interview = new Interview();
        //查询面试信息表
        List<Interview> interviews = interviewService.selectInterviewList(interview);

        for (Interview iv : interviews){
//            计算每条面试信息与当前日期的差值
            LocalDateTime createTime = iv.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            long time = ChronoUnit.DAYS.between(createTime, LocalDateTime.now());
//            查询该用户的意向等级
            Long userId = iv.getUserId();
            SysUser sysUser = userService.selectSysUserByUserIdCustom(userId);
            Integer level = sysUser.getLevel();
            Integer userIdleState = iv.getUserIdleState();
            Long interviewId = iv.getInterviewId();
//            判断用户是否处于面试中(状态值为2或3)
            if (userIdleState != 1 && userIdleState != 4){
//            若超出保护期，则修改面试状态为无面试(1)
                Integer outTime = outTimeService.selectOutTimeByUserLevel(level).getOutTime();
                if ( outTime <= time ){
                    sysUser.setIdleState(1);
                    userService.updateUser(sysUser);
                    interviewService.deleteInterviewByInterviewId(interviewId);
                }
            }
        }
    }

    /**
     * 月底借支销账
     */
    public void repayment(){
        Borrowing borrowing = new Borrowing();
        List<Borrowing> borrowings = borrowingService.selectBorrowingList(borrowing);
        for (Borrowing b : borrowings){
            b.setUserId(b.getUserId());
            b.setBorrowedNumber(0);
            b.setBorrowedAmount(0);
            borrowingService.updateBorrowing(b);
        }
    }

    /**
     * 小时工工作满七天开启借支功能
     */
    public void borrowing(){
        List<SysUser> sysUsers = sysUserRoleService.selectUserByRoleId(4L);
        for (SysUser sysUser : sysUsers){
            JobTimeRecord jobTimeRecord = jobTimeRecordService.selectJobTimeRecordByUserId(sysUser.getUserId());
            //如果当前时间减去入职时间大于7天，则开启借支功能，将flag属性设置为1
            if (ChronoUnit.DAYS.between(jobTimeRecord.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(), LocalDateTime.now()) >= 7){
                sysUser.setFlag(1);
                userService.updateUser(sysUser);
//                随后往Borrwing表中插入数据，设置number和amount的值
                Borrowing borrowing = new Borrowing();
                borrowing.setUserId(sysUser.getUserId());
                borrowing.setBorrowedNumber(5);//默认允许借5次
                borrowing.setBorrowedAmount(3000);//默认允许借3000元
                borrowingService.insertBorrowing(borrowing);
            }
        }
    }
}
